﻿using System;
using System.Globalization;

namespace Borg.Framework
{
    public class MenuItemInfo
    {
        public string Group;
        public string Image;
        public string Name;
        public float Order;
        public bool Separator = false;

        public MenuItemInfo(string menuPath)
        {
            var tilde = menuPath.Split(new[] { '~' });
            var pipe = tilde[tilde.Length - 1].Split(new[] { '|' });

            Name = pipe[0];
            Separator = Name.Length == 0;

            var dblPound = pipe[1].Split(new[] { "##" }, StringSplitOptions.None);
            float.TryParse(dblPound[0], NumberStyles.Float, CultureInfo.InvariantCulture, out Order);

            if (dblPound.Length > 1)
                Image = dblPound[1];

            if (tilde.Length > 1)
                Group = tilde[0].Split(new[] {'|'})[0];
        }

        public static string GetMenuPath(string menu, float menuOrder, PlugInfo plug)
        {
            return GetMenuPath(menu, menuOrder, plug.PlugTypeInfo);
        }

        public static string GetMenuPath(string menu, float menuOrder, PlugTypeInfo plugType)
        {
            return GetMenuPath(menu, menuOrder, (string)plugType.GetParamValue("Name"),
                (float)plugType.GetParamValue("OrderIndex"), (string)plugType.GetParamValue("Icon"));
        }

        public static string GetMenuPath(string menu, float menuOrder, string item, float itemOrder, string icon)
        {
            var menuPath = string.Format(CultureInfo.InvariantCulture,
                "{0}|{1}~{2}|{3}", new object[] { menu, menuOrder, item, itemOrder });

            if (icon != null)
                menuPath = menuPath + string.Format(CultureInfo.InvariantCulture,
                    "##{0}", new object[] {icon});

            return menuPath;
        }
    }
}

